package com.paic.mhis.hcpms.productClear.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.hcpms.productClear.biz.service.IProductClearService;
import com.paic.mhis.hcpms.productClear.constants.ProductClearConstants;
import com.paic.mhis.hcpms.productClear.dto.ProductClearDTO;
import com.paic.mock.util.StringUtils;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

@Controller
@RequestMapping("/exportProductClearController/")
public class ExportProductClearController {
	private Log log = LogFactory.getLog(this.getClass());

	@BizServicesProxy(beanName = "productClearServiceImpl")
	private IProductClearService productClearService;

	@RequestMapping(value = "findAttendantInfoCount.do")
	@ResponseBody
	public Map<String, Object> findAttendantInfoCount(HttpServletRequest request) {

		String caseCode = request.getParameter("caseCode");
		String orgCode = request.getParameter("orgCode");
		String clearTimeStart = request.getParameter("clearTimeStart");
		String clearTimeEnd = request.getParameter("clearTimeEnd");
		String approveTimeStart = request.getParameter("approveTimeStart");
		String approveTimeEnd = request.getParameter("approveTimeEnd");
		String caseState = request.getParameter("caseState");

		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("orgCode", orgCode);
		parameterMap.put("caseCode", caseCode);
		parameterMap.put("clearTimeStart", clearTimeStart);
		parameterMap.put("clearTimeEnd", clearTimeEnd);
		parameterMap.put("approveTimeStart", approveTimeStart);
		parameterMap.put("approveTimeEnd", approveTimeEnd);

		if (StringUtils.hasText(caseState)) {
			if (ProductClearConstants.status_001.equals(caseState)) {
				parameterMap.put("caseState", "'" + ProductClearConstants.status_7 + "','"
						+ ProductClearConstants.status_N2014009 + "'");
			} else {
				parameterMap.put("caseState", "'" + caseState + "'");
			}

		}

		long count = this.productClearService.getCountProductClearDTOByParamter(parameterMap);

		Map<String, Object> returnMap = new HashMap<String, Object>();

		returnMap.put("count", count);
		return returnMap;
	}

	private static List<String> productClearHeaderList = new ArrayList<String>();
	static {

		productClearHeaderList.add("编码");
		productClearHeaderList.add("机构名称");
		productClearHeaderList.add("费用");
		productClearHeaderList.add("审批日期");
		productClearHeaderList.add("结算日期");
		productClearHeaderList.add("案件状态");

	}

	@RequestMapping(value = "downLoadProductClear.do")
	@ResponseBody
	public void exportRules(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		try {
			String caseCode = request.getParameter("caseCode");
			String orgCode = request.getParameter("orgCode");
			String clearTimeStart = request.getParameter("clearTimeStart");
			String clearTimeEnd = request.getParameter("clearTimeEnd");
			String approveTimeStart = request.getParameter("approveTimeStart");
			String approveTimeEnd = request.getParameter("approveTimeEnd");
			String caseState = request.getParameter("caseState");

			parameterMap.put("orgCode", orgCode);
			parameterMap.put("caseCode", caseCode);
			parameterMap.put("clearTimeStart", clearTimeStart);
			parameterMap.put("clearTimeEnd", clearTimeEnd);
			parameterMap.put("approveTimeStart", approveTimeStart);
			parameterMap.put("approveTimeEnd", approveTimeEnd);

			if (StringUtils.hasText(caseState)) {
				if (ProductClearConstants.status_001.equals(caseState)) {
					parameterMap.put("caseState", "'" + ProductClearConstants.status_7 + "','"
							+ ProductClearConstants.status_N2014009 + "'");
				} else {
					parameterMap.put("caseState", "'" + caseState + "'");
				}

			}

			List<ProductClearDTO> productClearDTOList = this.productClearService
					.getProductClearDTOByParameter(parameterMap);

			String excelName = "费用结算清单";

			exportExcel(productClearDTOList, excelName, productClearHeaderList, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void exportExcel(List<ProductClearDTO> resultList, String excelName, List<String> productClearHeaderList,
			HttpServletResponse response) throws IOException {

		Workbook excel = null;
		try {
			excel = new HSSFWorkbook();
			Sheet sheet = excel.createSheet(excelName);
			Row row = sheet.createRow(0);
			// 设置Excel头
			int j = 0;
			for (String key : productClearHeaderList) {
				row.createCell(j).setCellValue(key);
				sheet.setColumnWidth(j, 4000);
				j++;
			}
			// 设置Excel内容
			if (null != resultList) {
				for (int i = 0; i < resultList.size(); i++) {

					ProductClearDTO bean = resultList.get(i);

					row = sheet.createRow(i + 1);
					row.createCell(0).setCellValue(bean.getCode());

					row.createCell(1).setCellValue(bean.getOrgName());
					Cell currentCell=row.createCell(2);
					currentCell.setCellValue(bean.getPay().doubleValue());
					

					HSSFCellStyle  cellStyle = (HSSFCellStyle) excel.createCellStyle();
					HSSFDataFormat format = (HSSFDataFormat) excel.createDataFormat();
		            cellStyle.setDataFormat(format.getFormat("0.000"));
		            currentCell.setCellStyle(cellStyle);


					
					row.createCell(3).setCellValue(bean.getApproveDate());
					row.createCell(4).setCellValue(bean.getClearDate());

					String statusDesc = this.getStatusDesc(bean.getStatus());
					row.createCell(5).setCellValue(statusDesc);

				}
			}

			initResponse(response, excelName);
			excel.write(response.getOutputStream());
		} catch (IOException e) {
			log.error("导出Excel是发生异常,异常原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			response.getOutputStream().flush();
			response.getOutputStream().close();

		}
	}

	private String getStatusDesc(String status) {
		if (ProductClearConstants.status_N2014009.equals(status) || ProductClearConstants.status_7.equals(status)) {
			return ProductClearConstants.status_desc_01;
		} else if (ProductClearConstants.status_11.equals(status)) {
			return ProductClearConstants.status_desc_02;

		} else if (ProductClearConstants.status_12.equals(status)) {
			return ProductClearConstants.status_desc_03;

		}

		return "";
	}

	private void initResponse(HttpServletResponse response, String name) {
		try {
			response.addHeader("Pragma", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
			response.reset();
			response.setContentType("APPLICATION/OCTET-STREAM");
			String fileName = new String((name).getBytes("utf-8"), "ISO8859-1") + ".xls";
			response.setHeader("content-disposition", "attachment;fileName=\"" + fileName + "\"");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

}
